package com.mayee.me.core.common.utils.result;

import java.io.Serializable;

/**
 * @description: 统一返回，默认在 controller 层使用
 * @author: mayee
 * @create: 2020-12-27 07:51
 **/
public class R<T> implements Serializable {
    private static final long serialVersionUID = -9047054711241799591L;

    private final int code;
    private final String msg;
    private T data;

    private R(Result result) {
        this.code = result.getCode();
        this.msg = result.getMsg();
    }

    private R(T data) {
        this.code = Result.SUCCESS.getCode();
        this.msg = Result.SUCCESS.getMsg();
        this.data = data;
    }

    private R(T data, Result result) {
        this.code = result.getCode();
        this.msg = result.getMsg();
        this.data = data;
    }

    public static R success() {
        return new R<>(Result.SUCCESS);
    }

    public static R fail() {
        return new R<>(Result.FAIL);
    }

    public static <T> R<T> data(T data) {
        return new R<>(data);
    }

    public static <T> R<T> data(T data, Result result) {
        return new R<>(data, result);
    }

}
